package com.ruoyi.community.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.community.domain.OwnerGroup;
import com.ruoyi.community.mapper.OwnerGroupMapper;
import com.ruoyi.community.mapper.OwnerGroupMemberMapper;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.community.domain.OwnerGroupMember;
import com.ruoyi.community.service.IOwnerGroupMemberService;
import com.ruoyi.system.mapper.SysUserMapper;

/**
 * 群组成员列表Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-05
 */
@Service
public class OwnerGroupMemberServiceImpl implements IOwnerGroupMemberService 
{

    @Autowired
    private OwnerGroupMemberMapper ownerGroupMemberMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private OwnerGroupMapper ownerGroupMapper;

    /**
     * 查询群组成员记录
     *
     * @param id 群组成员记录ID
     * @return 群组成员列表
     */
    @Override
    public OwnerGroupMember selectOwnerGroupMemberById(String id)
    {
        return ownerGroupMemberMapper.selectOwnerGroupMemberById(id);
    }

    @Override
    public boolean isInGroup(String userId, String groupId)
    {
        return ownerGroupMemberMapper.isInGroup(userId, groupId) == 1;
    }

    /**
     * 查询群组成员列表
     * 
     * @param ownerGroupMember 群组成员列表
     * @return 群组成员列表
     */
    @Override
//    public List<Map<String,Object>> selectOwnerGroupMemberList(OwnerGroupMember ownerGroupMember)
    public List<Map<String,Object>> selectOwnerGroupMemberList(String id, String userName, String phonenumber)
    {
        List<OwnerGroupMember> ownerGroupMembers = ownerGroupMemberMapper.selectOwnerGroupMemberList(id);
        List<Map<String,Object>> listMap = new ArrayList<Map<String,Object>>();
//        List<SysUser> sysUsers = new ArrayList<SysUser>();
        for (OwnerGroupMember m: ownerGroupMembers){
            Map<String,Object> map = new HashMap<String,Object>();
//            SysUser u = sysUserMapper.selectUserById(m.getUserId(), SecurityUtils.getCommunityId());
            SysUser query = new SysUser();
            query.setUserId(m.getUserId());
            List<SysUser> u_list = sysUserMapper.selectUserOnlyOnUser(query);
            SysUser u = u_list.get(0);
            if(userName != null && (!u.getUserName().contains(userName)&&!u.getNickName().contains(userName))){
                continue;
            }
            if(phonenumber != null && !u.getPhonenumber().contains(phonenumber)){
                continue;
            }
            map.put("ownerGroupMember", m);
            map.put("sysUser", u);
            listMap.add(map);
//            sysUsers.add(sysUserMapper.selectUserById(m.getUserId()));
        }
//        Map<String, Object> map = new HashMap<String, Object>();
//        map.put("ownerGroupMember", ownerGroupMembers);
//        map.put("sysUsers", sysUsers);
        return listMap;
    }

    /**
     * 新增群组成员
     *
     * @param ownerGroupMember 群组成员列表
     * @return 结果
     */
    @Override
    public int insertOwnerGroupMember(OwnerGroupMember ownerGroupMember)
    {
//        添加群组成员的时候群组member字段+1
        OwnerGroup ownerGroup = ownerGroupMapper.selectOwnerGroupById(ownerGroupMember.getGroupId());
        long num = ownerGroup.getMember();
        num ++;
        ownerGroup.setMember(num);
        ownerGroupMapper.updateOwnerGroup(ownerGroup);
        return ownerGroupMemberMapper.insertOwnerGroupMember(ownerGroupMember);
    }

//    /**
//     * 修改群组成员列表
//     *
//     * @param ownerGroupMember 群组成员列表
//     * @return 结果
//     */
//    @Override
//    public int updateOwnerGroupMember(OwnerGroupMember ownerGroupMember)
//    {
//        return ownerGroupMemberMapper.updateOwnerGroupMember(ownerGroupMember);
//    }

    /**
     * 批量删除群组成员列表
     *
     * @param ids 需要删除的群组成员列表ID
     * @return 结果
     */
    @Override
    public int deleteOwnerGroupMemberByIds(String[] ids)
    {
        OwnerGroupMember ownerGroupMember = ownerGroupMemberMapper.selectOwnerGroupMemberById(ids[0]);
        OwnerGroup ownerGroup = ownerGroupMapper.selectOwnerGroupById(ownerGroupMember.getGroupId());
        long num = ownerGroup.getMember();
        num = num - ids.length;
        ownerGroup.setMember(num);
        ownerGroupMapper.updateOwnerGroup(ownerGroup);
        return ownerGroupMemberMapper.deleteOwnerGroupMemberByIds(ids);
    }

    /**
     * 删除群组成员列表信息
     *
     * @param id 群组成员列表ID
     * @return 结果
     */
    @Override
    public int deleteOwnerGroupMemberById(String id)
    {
//        OwnerGroup ownerGroup = ownerGroupMapper.selectOwnerGroupById(id);
//        long num = ownerGroup.getMember();
//        num --;
//        ownerGroup.setMember(num);
//        ownerGroupMapper.updateOwnerGroup(ownerGroup);
        return ownerGroupMemberMapper.deleteOwnerGroupMemberById(id);
    }
}
